using UnityEngine;

public class ForceFieldObj3D : MonoBehaviour
{
    [SerializeField] private Transform center;
    private Vector3 gravitationPos;
    [SerializeField] public float forceRadius = 4;
    [SerializeField] private float repulsion;
    [SerializeField] private float targetAttractive;
    [SerializeField] private float speed = 0.1f;

    void Start()
    {
        gravitationPos = transform.position;
    }

    void Update()
    {
        if (forceRadius == 0) return;
        var velocity = Vector3.zero;
        var len = (transform.position - center.position).magnitude;
        if (len < forceRadius)
        {
            var rate = (forceRadius - len) / forceRadius;
            var intensity = repulsion * rate;
            velocity += (transform.position - center.position) * intensity * Time.deltaTime;
        }

        velocity += (gravitationPos - transform.position) * Time.deltaTime * targetAttractive * speed;
        transform.position += velocity;
        transform.localScale = Mathf.Clamp01((transform.position - center.position).magnitude / forceRadius) * Vector3.one;
    }
}